﻿using Microsoft.Graphics.Canvas.Effects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel.DataTransfer;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Documents;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using 纸片人.InterfaceFolder;
using 纸片人.win2dEffectControl;
using 纸片人.win2dEffectControl.DataClass;

// The Templated Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234235

namespace 纸片人.CalculateControls
{
    public sealed class MultiplyControl : OperatorControl
    {




        protected override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
          
        }

        protected override void Timer_Tick(object sender, object e)
        {

            if (oneConnectElement == null || twoConnectElement == null)
            {
                return;
            }

            GetOneExportValue();
            GetTwoExportValue();


            try
            {

                if (oneExportValue is double)
                {
                    if (twoExportValue is double)
                    {
                        Value = (double)oneExportValue * (double)twoExportValue;
                        Type = OutputType.Double;
                    }
                    else if (twoExportValue is Vector2)
                    {
                        Value = Vector2.Multiply(new Vector2((float)oneExportValue), (Vector2)twoExportValue);
                        Type = OutputType.Vector2;

                    }
                    else if (twoExportValue is Vector3)
                    {

                        Value = Vector3.Multiply(new Vector3((float)oneExportValue), (Vector3)twoExportValue);
                        Type = OutputType.Vector3;
                    }
                    else if (twoExportValue is Vector4)
                    {
                        Value = Vector4.Multiply(new Vector4((float)oneExportValue), (Vector4)twoExportValue);
                        Type = OutputType.Vector4;
                    }
                    else if (twoExportValue is Color)
                    {
                        Value = Vector4.Multiply(new Vector4((float)oneExportValue), (Vector4)twoExportValue);
                        Type = OutputType.Vector4;
                    }
                    else
                    {
                        return;
                    }
                }
                else if (oneExportValue is Vector2)
                {
                    if (twoExportValue is double)
                    {
                        Value = Vector2.Multiply((Vector2)oneExportValue, (float)twoExportValue);
                        Type = OutputType.Vector2;
                    }
                    else if (twoExportValue is Vector2)
                    {
                        Value = Vector2.Multiply((Vector2)oneExportValue, (Vector2)twoExportValue);
                        Type = OutputType.Vector2;

                    }
                    else if (twoExportValue is Vector3)
                    {
                        var v2 = (Vector2)oneExportValue;
                        Value = v2.Multiply((Vector3)twoExportValue);
                        Type = OutputType.Vector2;
                    }
                    else if (twoExportValue is Vector4)
                    {
                        var v2 = (Vector2)oneExportValue;
                        Value = v2.Multiply((Vector4)twoExportValue);
                        Type = OutputType.Vector2;
                    }
                    else
                    {
                        return;
                    }
                }
                else if (oneExportValue is Vector3)
                {
                    if (twoExportValue is double)
                    {
                        Value = Vector3.Multiply((Vector3)oneExportValue, (float)twoExportValue);
                        Type = OutputType.Vector3;
                    }
                    else if (twoExportValue is Vector2)
                    {
                        Value = Vector3.Multiply((Vector3)oneExportValue, new Vector3((Vector2)twoExportValue, 1));
                        Type = OutputType.Vector3;

                    }
                    else if (twoExportValue is Vector3)
                    {

                        Value = Vector3.Multiply((Vector3)oneExportValue, (Vector3)twoExportValue);
                        Type = OutputType.Vector3;
                    }
                    else if (twoExportValue is Vector4)
                    {
                        var v4 = (Vector4)twoExportValue;
                        Value = Vector3.Multiply((Vector3)oneExportValue, new Vector3(v4.X, v4.Y, v4.Z));
                        Type = OutputType.Vector3;
                    }
                    else
                    {
                        return;
                    }
                }
                else if (oneExportValue is Vector4)
                {
                    if (twoExportValue is double)
                    {
                        Value = Vector4.Multiply((Vector4)oneExportValue, (float)twoExportValue);
                        Type = OutputType.Vector4;
                    }
                    else if (twoExportValue is Vector2)
                    {
                        Value = Vector4.Multiply((Vector4)oneExportValue, new Vector4((Vector2)twoExportValue, 1, 1));
                        Type = OutputType.Vector4;

                    }
                    else if (twoExportValue is Vector3)
                    {

                        Value = Vector4.Multiply((Vector4)oneExportValue, new Vector4((Vector3)twoExportValue, 1));
                        Type = OutputType.Vector4;
                    }
                    else if (twoExportValue is Vector4)
                    {
                        var v4 = (Vector4)twoExportValue;
                        Value = Vector4.Multiply((Vector4)oneExportValue, v4);
                        Type = OutputType.Vector4;
                    }
                    else
                    {
                        return;
                    }
                }
                else
                {
                    return;
                }



            }
            catch
            {
                twoConnectElement.Exporter.Disconnect(twoConnectElement);
            }

        }


        void GetOneExportValue()
        {
            if (oneConnectElement != null)
            {
                try
                {
                    oneExportValue = GetProvideValue(oneConnectElement.Exporter, oneExportValueName);
                }
                catch
                {
                    oneConnectElement.Exporter.Disconnect(oneConnectElement);
                }
            }
        }

        private void GetTwoExportValue()
        {
            if (twoConnectElement != null)
            {
                try
                {
                    twoExportValue = GetProvideValue(twoConnectElement.Exporter, twoExportValueName);
                }
                catch
                {
                    twoConnectElement.Exporter.Disconnect(twoConnectElement);
                    
                }
            }
        }


        protected override void OnOneProviderDrop(object sender)
        {
            var outputData = EffectOutputControl.outputData;
            //判断是否形成了闭环了
            if (IsChartLoop(outputData.Exporter))
                return;
            if (outputData != null)
            {
                if (outputData.ExporterInfo is string str)
                {
                    oneExportValueName = str;
                }
                else
                {
                    return;
                }
                if (twoConnectElement != null)

                {
                    try
                    {
                        object temp;
                        oneExportValue = GetProvideValue(outputData.Exporter, oneExportValueName);

                        if (oneExportValue is double)
                        {
                            if (twoExportValue is double)
                            {

                            }
                            else if (twoExportValue is Matrix3x2)
                            {

                            }
                            else if (twoExportValue is Matrix4x4)
                            {

                            }
                            else if (twoExportValue is Vector2)
                            {

                            }
                            else if (twoExportValue is Vector3)
                            {

                            }
                            else if (twoExportValue is Vector4)
                            {

                            }
                        }
                        #region 向量
                        else if (oneExportValue is Vector2)
                        {
                            if (twoExportValue is double)
                            {

                            }
                            else if (twoExportValue is Vector2)
                            {

                            }
                        }
                        else if (oneExportValue is Vector3)
                        {
                            if (twoExportValue is double)
                            {

                            }
                            else if (twoExportValue is Vector3)
                            {

                            }
                        }
                        else if (oneExportValue is Vector4)
                        {
                            if (twoExportValue is double)
                            {

                            }
                            else if (twoExportValue is Vector4)
                            {

                            }

                        }
                        #endregion
                        #region
                        else if (oneExportValue is Matrix3x2)
                        {
                            if (twoExportValue is double)
                            {

                            }
                            else if (twoExportValue is Matrix3x2)
                            {

                            }
                        }

                        else if (oneExportValue is Matrix4x4)
                        {
                            if (twoExportValue is double)
                            {

                            }
                            else if (twoExportValue is Matrix4x4)
                            {

                            }
                        }
                        #endregion
                    }
                    catch
                    {
                        return;
                    }

                }
                oneExportType = outputData.Exporter.Type;
                CreateConnect( ref oneConnectElement, (ProviderDropControl)sender);
                oneExportValue = GetProvideValue(outputData.Exporter, oneExportValueName);
            }
        }

        protected override void OnTwoProviderDrop(object sender)
        {
            var outputData = EffectOutputControl.outputData;
            //判断是否形成了闭环了
            if (IsChartLoop(outputData.Exporter))
                return;
            if (outputData != null)
            {


                if (outputData.ExporterInfo is string str)
                {
                    twoExportValueName = str;
                }
                else
                {
                    return;
                }
                if (oneConnectElement != null)
                {
                    try
                    {
                        twoExportValue = GetProvideValue(outputData.Exporter, twoExportValueName);

                        if (oneExportValue is double)
                        {
                            if (twoExportValue is double)
                            {
                                Value = (double)oneExportValue + (double)twoExportValue;
                                Type = OutputType.Double;
                            }

                            else if (twoExportValue is Vector2)
                            {
                                Vector2.Multiply((float)oneExportValue, (Vector2)twoExportValue);
                                Type = OutputType.Vector2;

                            }
                            else if (twoExportValue is Vector3)
                            {
                                Vector3.Multiply((float)oneExportValue, (Vector3)twoExportValue);
                                Type = OutputType.Vector3;


                            }
                            else if (twoExportValue is Vector4)
                            {
                                Vector4.Multiply((float)oneExportValue, (Vector4)twoExportValue);
                                Type = OutputType.Vector4;

                            }
                            else
                            {
                                return;
                            }
                        }
                        #region 向量
                        else if (oneExportValue is Vector2)
                        {
                            if (twoExportValue is Vector2)
                            {
                                Vector2.Multiply((Vector2)oneExportValue, (Vector2)twoExportValue);
                                Type = OutputType.Vector2;

                            }
                            else
                            {
                                return;
                            }
                        }
                        else if (oneExportValue is Vector3)
                        {
                            if (twoExportValue is Vector3)
                            {
                                Vector3.Multiply((Vector3)oneExportValue, (Vector3)twoExportValue);
                                Type = OutputType.Vector3;
                            }
                        }
                        else if (oneExportValue is Vector4)
                        {
                            if (twoExportValue is Vector4)
                            {
                                Vector4.Multiply((Vector4)oneExportValue, (Vector4)twoExportValue);
                                Type = OutputType.Vector4;
                            }
                            else
                            {
                                return;
                            }
                        }
                        #endregion
                        #region
                        else if (oneExportValue is Matrix3x2)
                        {
                            if (twoExportValue is double)
                            {
                                Matrix3x2.Multiply((Matrix3x2)oneExportValue, (float)twoExportValue);
                                Type = OutputType.Matrix3x3;

                            }
                            else if (twoExportValue is Matrix3x2)
                            {
                                Matrix3x2.Multiply((Matrix3x2)oneExportValue, (Matrix3x2)twoExportValue);
                                Type = OutputType.Matrix3x3;

                            }
                            else
                            {
                                return;
                            }
                        }

                        else if (oneExportValue is Matrix4x4)
                        {
                            if (twoExportValue is double)
                            {
                                Matrix4x4.Multiply((Matrix4x4)oneExportValue, (float)twoExportValue);
                                Type = OutputType.Matrix4x4;
                            }
                            else if (twoExportValue is Matrix4x4)
                            {
                                Matrix4x4.Multiply((Matrix4x4)oneExportValue, (Matrix4x4)twoExportValue);
                                Type = OutputType.Matrix4x4;
                            }
                            else
                            {
                                return;
                            }
                        }
                        #endregion
                    }
                    catch
                    {
                        twoConnectElement.Exporter.Disconnect(twoConnectElement);
                        
                    }

                }

                twoExportType = outputData.Exporter.Type;
                CreateConnect(ref twoConnectElement, (ProviderDropControl)sender);
                twoExportValue = GetProvideValue(outputData.Exporter, twoExportValueName);
            }
        }

        protected override void OnProviderDragEnter(object sender, DragEventArgs e)
        {
            var outputData = EffectOutputControl.outputData;
            if (outputData != null)
            {
                e.AcceptedOperation = DataPackageOperation.Copy;
                e.DragUIOverride.Caption = "拖进来了";
            }
        }
    }
}
